<?php

namespace App\Modules\ImportDorisData\Factory\DorisTables;

use App\Common\Librarys\Str;
use App\Modules\ImportDorisData\Factory\BaseTable;
use Swoole\Database\PDOPool;

class CallResultTable extends BaseTable
{
    const TYPE = 'call_result';

    /**
     * @var string[]
     */
    protected $fields = [
        'detail_id',
        'task_id',
        'task_type',
        'phone',
        'status',
        'result',
        'call_time',
    ];

    public function importData(PDOPool $pool): bool
    {
        try {
            $pdo = $pool->get();
            $sql = 'insert into call_result(' . implode(',', $this->fields) .') values(:'. implode(',:', $this->fields) .')';
            $p = $pdo->prepare($sql);
            for ($i = $this->start; $i <= $this->genDataLines; $i++) {
                $importData = [
                    'detail_id' => $i,
                    'task_id' => $i % 1000,
                    'task_type' => 1,
                    'phone' => Str::randomPhone(),
                    'status' => array_rand([1,2,3,4]),
                    'result' => array_rand(range(1, 13, 1)),
                    'call_time' => Str::randomDate('20210102','20220501'),
                ];

                foreach ($importData as $key => $value) {
                    $p->bindParam(':' . $key, $value);
                    unset($value);
                }
                $p->execute();
                sleep(5);
            }
        } catch (\Exception $exception) {
            echo "<pre>";
            print_r($p->debugDumpParams());
            echo $exception->getMessage();
        } finally {
            $pool->put($pdo);
        }
        return true;
    }
}